<script setup lang="ts">
import { useUser } from "@/store/useUser";
import defaultAvatar from "@/assets/app-mj.png";
import { computed } from "vue";
import { fileSize } from "@/utils/func";

const userStore = useUser();
const props = defineProps({
  msg: {
    type: Object,
    required: true,
  },
});

// 头像
const headSrc = computed(() => {
  if (isMine.value && userStore.getUserInfo.avatar) {
    return userStore.getUserInfo.avatar;
  }
  if (!isMine.value && props.msg.fromAvatar) {
    return props.msg.fromAvatar;
  }
  return defaultAvatar;
});
// 是不是自己的消息
const isMine = computed(() => {
  return props.msg.from === userStore.getUserInfo.uimKey;
});

// 预览文件
const previewFile = () => {
  console.log("previewFile", props.msg);
  const fileUrl = props.msg.content;
  if (fileUrl) {
    window.open(fileUrl);
  }
};
</script>

<template>
  <div class="text-msg">
    <el-image v-if="!isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="default-avatar" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
    <div class="msg-text" :class="{ mine: isMine }">
      <p class="name" v-if="!isMine">{{ props.msg.fromName }}</p>
      <div class="file" :class="{ mine: isMine }">
        <span class="file-span" title="点击预览" @click="previewFile">
          <img src="@/assets/file-icon.jpg" alt="" />
          <span class="file-info" v-if="props.msg.exData.fileName">
            <span class="name">{{ props.msg.exData.fileName }}</span>
            <span class="status-size"
              ><span class="status">文件大小：</span>
              <span>{{ fileSize(props.msg.exData.fileSize) }}</span></span
            >
          </span>
        </span>
      </div>
      <i v-if="isMine">{{ props.msg.read ? "已读" : "未读" }}</i>
    </div>
    <el-image v-if="isMine" class="head" :src="headSrc" alt="">
      <template #error>
        <div class="image-error">
          <img class="default-avatar" :src="defaultAvatar" alt="" />
        </div>
      </template>
    </el-image>
  </div>
</template>

<style scoped lang="scss">
.text-msg {
  display: flex;
  margin-bottom: 26px;
  .head {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    margin-right: 8px;
    border-radius: 50%;
  }
  .msg-text {
    width: 100%;
    &.mine {
      text-align: right;
      padding-right: 10px;
      .text {
        background: rgba(1, 154, 129, 1);
        color: #fff;
        border-radius: 12px 0 12px 12px;
      }
      i {
        display: block;
        font-style: normal;
        text-align: right;
        font-size: 12px;
        color: #999;
      }
    }
    .name {
      color: rgba(25, 30, 40, 0.36);
      font-size: 12px;
      margin-bottom: 4px;
    }
    .text {
      display: inline-block;
      background: #fff;
      padding: 12px 20px;
      border-radius: 0 12px 12px 12px;
      max-width: 80%;
      box-shadow: 0 4px 12px -2px rgba(11, 13, 18, 0.06);
    }
  }
  .file {
    display: flex;
    &.mine {
      justify-content: flex-end;
    }
    .file-span {
      cursor: pointer;
      display: flex;
      align-items: center;
      width: 300px;
      background: rgba(25, 30, 40, 0.03);
      border-radius: 12px 12px 12px 12px;
      padding: 9px;
      img {
        width: 60px;
        height: 60px;
        margin-right: 14px;
        border-radius: 8px;
      }
      .file-info {
        display: flex;
        flex-direction: column;
        .name {
          font-weight: bold;
          color: #303753;
          margin-bottom: 10px;
          text-align: left;
        }
        .status-size {
          color: #b3b9c7;
          font-size: 12px;
          text-align: left;
          display: flex;
          .status {
            flex: 1;
          }
        }
      }
    }
  }
}
</style>
